package cn.car.utils;

import cn.car.domains.enums.NvrType;
import cn.car.domains.info.RTSPUrlConfigInfo;
import cn.car.service.config.SystemConfig;
import com.alibaba.fastjson2.JSONObject;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@Slf4j
public class NvrUtil {

    @Resource
    private SystemConfig systemConfig;

    public String getNvrRtspUrl(String startTime, String endTime, String nvrAddressUrl, String channelCode) {
        final boolean livingType = StringUtils.isEmpty(startTime) || StringUtils.isEmpty(endTime);
        final JSONObject vars = new JSONObject()
                .fluentPut("username", systemConfig.getNvrUserName().get())
                .fluentPut("pwd", systemConfig.getNvrPassWord().get())
                .fluentPut("host", nvrAddressUrl)
                .fluentPut("channel", channelCode)
                .fluentPut("startTimeUTC", livingType ? "" : startTime.replace("t", "T").replace("z", "Z"))
                .fluentPut("endTimeUTC", livingType ? "" : endTime.replace("t", "T").replace("z", "Z"))

                .fluentPut("startTimeS", livingType ? "" : CarTimeUtil.strToTimestamp(startTime.replace("t", "").replace("z", ""), "yyyyMMddHHmmss") / 1000)
                .fluentPut("endTimeS", livingType ? "" : CarTimeUtil.strToTimestamp(endTime.replace("t", "").replace("z", ""), "yyyyMMddHHmmss") / 1000)
                .fluentPut("startTimeMS", livingType ? "" : CarTimeUtil.strToTimestamp(startTime.replace("t", "").replace("z", ""), "yyyyMMddHHmmss"))
                .fluentPut("endTimeMS", livingType ? "" : CarTimeUtil.strToTimestamp(endTime.replace("t", "").replace("z", ""), "yyyyMMddHHmmss"));

        final NvrType nvrTypeConfig = NvrType.getByName(systemConfig.getNvrType().get());
        final NvrType nvrType = Optional.ofNullable(nvrTypeConfig).orElseGet(() -> guessNvrTypeByChannelCode(channelCode));
        final RTSPUrlConfigInfo rtspUrlConfig = nvrType.getRtspUrlConfig();
        final String liveUrlDefault = rtspUrlConfig.getLiveUrl();
        final String replayUrlDefault = rtspUrlConfig.getReplayUrl();

        final String replayUrlCustom = systemConfig.getNvrUrl().get();
        final String liveUrlCustom = systemConfig.getLiveNvrUrl().get();

        final String rtspTemplate = livingType ?
                StringUtils.defaultIfBlank(liveUrlCustom, liveUrlDefault)
                : StringUtils.defaultIfBlank(replayUrlCustom, replayUrlDefault);

        final String rtspUrl = CarStringTemplateUtil.BRACES.replace(rtspTemplate, vars);

        log.info("[getNvrRtspUrl] nvrType:{}, livingType:{}, template:{}, rtspUrl:{}, vars:{}", nvrType, livingType, rtspTemplate, rtspUrl, vars.toJSONString());
        return rtspUrl;
    }

    private NvrType guessNvrTypeByChannelCode(String channelCode) {
        final NvrType defaultType = NvrType.HIKVISION;
        if (StringUtils.isEmpty(channelCode)) {
            return defaultType;
        }

        // 宇视的通道号，c开头
        if (channelCode.length() == 2 && channelCode.toLowerCase().startsWith("c")) {
            return NvrType.UNIVIEW;
        }

        return defaultType;
    }
}
